package cn.ynrcb.personnelfile.admin.api;


import cn.hutool.core.util.ObjectUtil;
import cn.ynrcb.personnelfile.admin.enums.SmsTypeEnum;
import cn.ynrcb.personnelfile.admin.service.SmsService;
import cn.ynrcb.personnelfile.admin.vo.SmsVo;
import cn.ynrcb.personnelfile.common.exception.MyException;
import cn.ynrcb.personnelfile.common.utils.R;
import cn.ynrcb.personnelfile.common.validator.ValidatorUtils;
import cn.ynrcb.personnelfile.common.validator.group.AddGroup;
import cn.ynrcb.personnelfile.sysadmin.controller.AbstractController;
import cn.ynrcb.personnelfile.sysadmin.entity.SysUserEntity;
import cn.ynrcb.personnelfile.sysadmin.service.CurrentUserService;
import cn.ynrcb.personnelfile.sysadmin.service.SysCaptchaService;
import cn.ynrcb.personnelfile.sysadmin.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 短信发送接口
 *
 * @author lihui
 * @date 2021-03-25 17:23:21
 */
@Api(value = "整改需求系统系统", tags = {"业务相关=>短信发送相关接口"})
@RestController
@RequestMapping("api/sms")
public class SmsController extends AbstractController {
    @Autowired
    private SmsService smsService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysCaptchaService sysCaptchaService;
    @Autowired
    private CurrentUserService currentUserService;

    /**
     * 短信发送
     *
     * @param params
     * @return
     */
    @ApiOperation("短信发送")
    @PostMapping("/send")
    public R sendSms(@RequestBody SmsVo.SendSmsParm params) {
        ValidatorUtils.validateEntity(params, AddGroup.class);
        //忘记密码短信发送时，验证该号码是否在系统中
        if (params.getType() == SmsTypeEnum.PSW_RESET) {
            SysUserEntity user = sysUserService.getOne(new QueryWrapper<SysUserEntity>().eq("mobile", params.getPhone()));
            if (ObjectUtil.isNull(user)) {
                throw new MyException("该手机号未绑定用户！");
            }
        }
        //绑定手机或更换手机短信发送时，验证该手机号是否被使用过
        if (params.getType() == SmsTypeEnum.BIND_MOBILE || params.getType() == SmsTypeEnum.REPLACE_MOBILE) {
            SysUserEntity userByMobile = sysUserService.getOne(new QueryWrapper<SysUserEntity>().eq("mobile", params.getPhone()));
            //绑定手机号使用过则不可绑定
            if (ObjectUtil.isNotNull(userByMobile)) {
                throw new MyException("该手机号已被使用！");
            }
        }
        //验证验证码
        boolean captcha = sysCaptchaService.validate(params.getUuid(), params.getCaptcha());
        if (!captcha) {
            return R.error(2001, "验证码不正确或已过期！");
        }
        return smsService.sendSms(params);
    }

    /**
     * 未登录短信发送
     *
     * @param params
     * @return
     */
    @ApiOperation("未登录短信发送")
    @PostMapping("/sendNT")
    public R sendSmsNoToken(@RequestBody SmsVo.SendSmsParm params) {
        ValidatorUtils.validateEntity(params, AddGroup.class);
        //忘记密码短信发送时，验证该号码是否在系统中
        if (params.getType() == SmsTypeEnum.PSW_RESET) {
            SysUserEntity user = sysUserService.getOne(new QueryWrapper<SysUserEntity>().eq("mobile", params.getPhone()).ne("status", -1));
            if (ObjectUtil.isNull(user)) {
                throw new MyException("该手机号未绑定用户！");
            }
        }
        //验证验证码
        boolean captcha = sysCaptchaService.validate(params.getUuid(), params.getCaptcha());
        if (!captcha) {
            return R.error(2001, "验证码不正确或已过期！");
        }
        return smsService.sendSms(params);
    }

    @ApiOperation("自主备案短信发送")
    @PostMapping("/sendMsg")
    public R sendMsg(@RequestBody SmsVo.SendSmsParm params) {
        ValidatorUtils.validateEntity(params, AddGroup.class);
        //验证验证码
        boolean captcha = sysCaptchaService.validate(params.getUuid(), params.getCaptcha());
        if (!captcha) {
            return R.error(2001, "验证码不正确或已过期！");
        }
        return smsService.sendMsg(params);
    }



}
